package b3.widget.basic
{
	import b3.util.*;
	
	import flash.display.*;
	import flash.events.*;
	
	import com.greensock.*;

	public class BasicWidget extends Sprite
	{
		//左顶
		public static const ANCHOR_LEFT_TOP:int = 0;
		//中顶
		public static const ANCHOR_MIDDLE_TOP:int = 1;
		//右顶
		public static const ANCHOR_RIGHT_TOP:int = 2;
		//左中
		public static const ANCHOR_LEFT_MIDDLE:int = 3;
		//中中
		public static const ANCHOR_MIDDLE_MIDDLE:int = 4;
		//右中
		public static const ANCHOR_RIGHT_MIDDLE:int = 5;
		//左底
		public static const ANCHOR_LEFT_BOTTOM:int = 6;
		//中底
		public static const ANCHOR_MIDDLE_BOTTOM:int = 7;
		//右底
		public static const ANCHOR_RIGHT_BOTTOM:int = 8;
		
		//效果
		public static const EFFECT_NONE:int = 0;
		//膨胀
		public static const EFFECT_EXPAND:int = 1;
		//上浮
		public static const EFFECT_FLOAT:int = 2;
		
		
		public var anchor:int;
		public var effect:int;
		private var initX:Number;
		private var initY:Number;
		private var firstAddedFlag:Boolean;
		
		public function BasicWidget()
		{
			super();
			firstAddedFlag = true;
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
        
		private function onAddedToStage(event:Event):void
		{
			stage.addEventListener(Event.FULLSCREEN, onFullScreen);
			if (firstAddedFlag)
			{
				initX = this.x;
				initY = this.y;
				firstAddedFlag = false;
			}
			onFullScreen(null);
			if (effect == EFFECT_EXPAND)
				TweenLite.from(this, 0.2, {width:0, height:0, x:x + width / 2, y:y + height / 2});
			else if (effect == EFFECT_FLOAT)
				TweenLite.from(this, 0.2, {alpha:0, y:y + 20});
		}

		private function onRemovedFromStage(event:Event):void
		{
			stage.removeEventListener(Event.FULLSCREEN, onFullScreen);
		}
		
		private function onFullScreen(event:Event):void
		{
			var anchorX:Number = (BFont.stageWidth / 2) * (anchor % 3);
			var anchorY:Number = (BFont.stageHeight / 2) * int(anchor / 3);
			this.x = anchorX + initX;
			this.y = anchorY + initY;
		}
	}
}